﻿/**
* Vista asociada al contenido 
* del tab de la funcionalidad VER LISTADO DE DOCUMENTOS->DETALLE DEL DOCUMENTO
* en el aplicativo.
*/
Ext.define('entgpv11.view.details.SourceView', {
    extend: 'Ext.Panel',
    xtype: 'sourceview',
    //requires: [
    //    'Ext.ux.SimpleIFrame'],
    config:
    {
        cls: 'ux-code',
        styleHtmlContent: false,
        //scroll: 'both',
        floating: true,
        //autoScroll: true,
        scrollable: {
            direction: 'both',
            directionLock: true
        },
        //centered: true,
        width: '100%',
        height: '100%'

    },
    initialize: function () {
        try {
            //this.applyScrollable({ direction: 'horizontal', directionLock: true });

        } catch (ex) {
            APP_GLOBAL.SetLog('DocList.initialize::' + ex);
        }
    },
    setViewHtml: function (data_txt, html_link) {
        try {
      
        
        this.setMasked(true);
        this.setHtml('<center>Cargando Documento...</center>');

        var v = '<div class="x-scroll-container"><iframe src="' + html_link + '" width=\"90%\" height=\"90%\" scrolling="no" frameborder="0" ></iframe></div>';
        var ifSetHml = false;
        var _name = html_link.substring(html_link.lastIndexOf('/') + 1);
        
        var extList = _name.substring(_name.lastIndexOf('.') + 1);
        
       if (extList == 'txt') {
                v = '<pre>' + data_txt + '</pre>';
                this.setHtml(v);
                ifSetHml = true;
       }
       if (extList == 'pdf') {
           v = '<div id="wrapper"><embed width="100%" height="100%" name="plugin"  id="iframe-' + this.id + '"' +
            ' height="100%" width="100%" ' +
            ' src="' + html_link + '"' +
            '></embed></div>';
           this.setHtml(v);
           var startY = 0;
           var startX = 0;
           var ifrDocument = document.getElementById("iframe-" + this.id);
           ifrDocument.addEventListener('touchstart', function (event) {
               try {
                   window.scrollTo(0, 1);
                   startY = event.targetTouches[0].pageY;
                   startX = event.targetTouches[0].pageX;
               }
               catch (e) {
                   throw ('ifrDocument.touchstart ' + e.message);
               }
           });
           ifrDocument.addEventListener('touchmove', function (event) {
               try {
                   event.preventDefault();
                   var posy = event.targetTouches[0].pageY;
                   var h = document.getElementById("wrapper");
                   var sty = h.scrollTop;

                   var posx = event.targetTouches[0].pageX;
                   var stx = h.scrollLeft;
                   h.scrollTop = sty - (posy - startY);
                   h.scrollLeft = stx - (posx - startX);
                   startY = posy;
                   startX = posx;
               }
               catch (e) {
                   throw ('ifrDocument.touchmove' + e.message);
               }
           });
       }          
       if (!ifSetHml) {
     
            v = '<div id="wrapper"><iframe id="iframe-' + this.id + '"' +
            ' height="100%" width="100%" scrolling="no" ' +
            ' style="border:none;" ' +
            ' src="' + html_link + '"' +
            '></iframe></div>';
            this.setHtml(v);
            var startY = 0;
            var startX = 0;
            var ifrDocument = document.getElementById("iframe-" + this.id).contentWindow.document;
            ifrDocument.addEventListener('touchstart', function (event) {
               try{
                    window.scrollTo(0, 1);
                    startY = event.targetTouches[0].pageY;
                    startX = event.targetTouches[0].pageX;
                }
                catch (e) {
                    throw ('ifrDocument.touchstart ' + e.message);
                }
            });
            ifrDocument.addEventListener('touchmove', function (event) {
               try{
                    event.preventDefault();
                    var posy = event.targetTouches[0].pageY;
                    var h = document.getElementById("wrapper");
                    var sty = h.scrollTop;

                    var posx = event.targetTouches[0].pageX;
                    var stx = h.scrollLeft;
                    h.scrollTop = sty - (posy - startY);
                    h.scrollLeft = stx - (posx - startX);
                    startY = posy;
                    startX = posx;
                 }
                 catch (e) {
                     throw ('ifrDocument.touchmove' + e.message);
                 }
            });

        }
        this.setMasked(false);
        } catch (ex) {
        APP_GLOBAL.SetLog('DocList.setViewHtml::' + ex);
        }

    }
});